package com.exampl.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Autowired
    HttpServletRequest request;  // 使用注入的方法获取request对象，进而获取session

    @Override
    public void insertFill(MetaObject metaObject) {
        // metaObject即为进行了insert操作时传递过来的数据
        // 设置时间
        metaObject.setValue("createTime", new Date());
        metaObject.setValue("updateTime", new Date());
        // 设置修改人
//        metaObject.setValue("createUser", request.getSession().getAttribute("employee"));
//        metaObject.setValue("updateUser", request.getSession().getAttribute("employee"));
        metaObject.setValue("createUser", BaseContext.getCurrentId());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // metaObject即为进行了update操作时传递过来的数据
        // 设置时间
        metaObject.setValue("updateTime", new Date());
        // 设置修改人
//        metaObject.setValue("updateUser", request.getSession().getAttribute("employee"));
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }
}
